package com.ruoyi.wcf.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.wcf.domain.WcfMobileSeries;
import com.ruoyi.wcf.mapper.WcfMobileSeriesMapper;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wcf.mapper.WcfMobileModelMapper;
import com.ruoyi.wcf.domain.WcfMobileModel;
import com.ruoyi.wcf.service.IWcfMobileModelService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 手机型号Service业务层处理
 *
 * @author WZP
 * @date 2022-04-02
 */
@Service
public class WcfMobileModelServiceImpl implements IWcfMobileModelService
{
    @Autowired
    private WcfMobileModelMapper wcfMobileModelMapper;
    @Autowired
    private WcfMobileSeriesMapper mobileSeriesMapper;

    /**
     * 查询手机型号
     *
     * @param modelId 手机型号主键
     * @return 手机型号
     */
    @Override
    public WcfMobileModel selectWcfMobileModelByModelId(Long modelId)
    {
        return wcfMobileModelMapper.selectWcfMobileModelByModelId(modelId);
    }

    /**
     * 查询手机型号列表
     *
     * @param wcfMobileModel 手机型号
     * @return 手机型号
     */
    @Override
    public List<WcfMobileModel> selectWcfMobileModelList(WcfMobileModel wcfMobileModel)
    {
        return wcfMobileModelMapper.selectWcfMobileModelList(wcfMobileModel);
    }

    /**
     * 新增手机型号
     *
     * @param wcfMobileModel 手机型号
     * @return 结果
     */
    @Override
    public int insertWcfMobileModel(WcfMobileModel wcfMobileModel){
        return wcfMobileModelMapper.insertWcfMobileModel(wcfMobileModel);
    }

    /**
     * 修改手机型号
     *
     * @param wcfMobileModel 手机型号
     * @return 结果
     */
    @Override
    public int updateWcfMobileModel(WcfMobileModel wcfMobileModel)
    {
        wcfMobileModel.setUpdateAt(new Date());
        return wcfMobileModelMapper.updateWcfMobileModel(wcfMobileModel);
    }

    /**
     * 批量删除手机型号
     *
     * @param modelIds 需要删除的手机型号主键
     * @return 结果
     */
    @Override
    public int deleteWcfMobileModelByModelIds(Long[] modelIds)
    {
        return wcfMobileModelMapper.deleteWcfMobileModelByModelIds(modelIds);
    }

    /**
     * 删除手机型号信息
     *
     * @param modelId 手机型号主键
     * @return 结果
     */
    @Override
    public int deleteWcfMobileModelByModelId(Long modelId)
    {
        return wcfMobileModelMapper.deleteWcfMobileModelByModelId(modelId);
    }

    /**
     * 新增手机型号
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addWcfMobileModel(Map<String,Object> map) {
        Long brandId = MapUtils.getLong(map, "brandId");
        String seriesName = MapUtils.getString(map, "seriesName");
        String modelName = MapUtils.getString(map, "modelName");
        Integer status = MapUtils.getInteger(map, "status");

        WcfMobileModel wcfMobileModel = new WcfMobileModel();

        WcfMobileSeries series = mobileSeriesMapper.getBySeriesName(seriesName,brandId);
        if (ObjectUtils.isNotEmpty(series)){
            //如果此系列存在
            wcfMobileModel.setParentId(series.getSeriesId());
        }else{
            //如果不存在则新增系列
            WcfMobileSeries mobileSeries = new WcfMobileSeries();
            mobileSeries.setParentId(brandId);
            mobileSeries.setSeriesName(seriesName);
            mobileSeries.setStatus(0);
            mobileSeries.setCreateAt(new Date());
            mobileSeries.setDelFlag(0);
            mobileSeriesMapper.insertWcfMobileSeries(mobileSeries);

            wcfMobileModel.setParentId(mobileSeries.getSeriesId());
        }

        wcfMobileModel.setBrandId(brandId);
        wcfMobileModel.setModelName(modelName);
        wcfMobileModel.setStatus(status);
        wcfMobileModel.setCreateAt(new Date());
        wcfMobileModel.setDelFlag(0);
        int i = wcfMobileModelMapper.insertWcfMobileModel(wcfMobileModel);
        if (i == 0){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return i;
    }

    /**
     * 修改手机型号
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int editWcfMobileModel(Map<String,Object> map) {
        Long brandId = MapUtils.getLong(map, "brandId");
        String seriesName = MapUtils.getString(map, "seriesName");
        Long modelId = MapUtils.getLong(map, "modelId");
        String modelName = MapUtils.getString(map, "modelName");
        Integer status = MapUtils.getInteger(map, "status");

        //查询型号详情
        WcfMobileModel wcfMobileModel = wcfMobileModelMapper.selectWcfMobileModelByModelId(modelId);

        //查询原本系列
        WcfMobileSeries mobileSeries = mobileSeriesMapper.selectWcfMobileSeriesBySeriesId(wcfMobileModel.getParentId());

        //修改手机系列
        if (ObjectUtils.isNotEmpty(mobileSeries) && mobileSeries.getSeriesName().equals(seriesName)){
            wcfMobileModel.setParentId(mobileSeries.getSeriesId());
        }else{
            WcfMobileSeries series = mobileSeriesMapper.getBySeriesName(seriesName,brandId);
            if (ObjectUtils.isNotEmpty(series)){
                //如果此系列存在
                wcfMobileModel.setParentId(series.getSeriesId());
            }else{
                //如果不存在则新增系列
                WcfMobileSeries newSeries = new WcfMobileSeries();
                newSeries.setParentId(brandId);
                newSeries.setSeriesName(seriesName);
                newSeries.setStatus(0);
                newSeries.setCreateAt(new Date());
                newSeries.setDelFlag(0);
                mobileSeriesMapper.insertWcfMobileSeries(newSeries);

                wcfMobileModel.setParentId(newSeries.getSeriesId());
            }
        }

        wcfMobileModel.setBrandId(brandId);
        wcfMobileModel.setModelName(modelName);
        wcfMobileModel.setStatus(status);
        wcfMobileModel.setUpdateAt(new Date());
        wcfMobileModel.setDelFlag(0);

        int i = wcfMobileModelMapper.updateWcfMobileModel(wcfMobileModel);
        if (i == 0){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return i;
    }

    /**
     * 查询手机型号详情
     *
     * @param modelId 手机型号主键
     * @return 手机型号
     */
    @Override
    public Map<String,Object> getDetail(Long modelId){
        return wcfMobileModelMapper.getDetail(modelId);
    }

    /**
     * 获取手机型号下拉列表
     * @param map
     * @return
     */
    @Override
    public List<WcfMobileModel> getSelectList(Map<String,Object> map){
        return wcfMobileModelMapper.getSelectList(map);
    }

    /**
     * app-产品检索-手机型号列表
     * @param map
     * @return
     */
    @Override
    public List<WcfMobileModel> mobileModelList(Map<String,Object> map){
        return wcfMobileModelMapper.mobileModelList(map);
    }

}
